package com.atguigu.service.impl;

import com.atguigu.dao.DockerServiceConfigDao;
import com.atguigu.dao.impl.DockerServiceConfigDaoImpl;
import com.atguigu.pojo.DockerServiceConfig;
import com.atguigu.pojo.Page;
import com.atguigu.service.DockerServiceConfigService;

import java.util.List;

public class DockerServiceConfigServiceImpl implements DockerServiceConfigService {
    DockerServiceConfigDao dockerServiceConfigDao=new DockerServiceConfigDaoImpl();
    @Override
    public void addDockerServiceConfig(DockerServiceConfig dockerServiceConfig) {
        dockerServiceConfigDao.addDockerServiceConfig(dockerServiceConfig);
    }

    @Override
    public void updateDockerServiceConfig(DockerServiceConfig dockerServiceConfig) {
        dockerServiceConfigDao.updateDockerServiceConfig(dockerServiceConfig);
    }

    @Override
    public void deleteDockerServiceConfigById(String id) {
        dockerServiceConfigDao.deleteDockerServiceConfigById(id);
    }

    @Override
    public DockerServiceConfig queryDockerServiceConfigById(String id) {
        return dockerServiceConfigDao.queryDockerServiceConfigById(id);
    }

    @Override
    public List<DockerServiceConfig> queryDockerServiceConfigs() {
        return dockerServiceConfigDao.queryDockerServiceConfigs();
    }
    @Override
    public DockerServiceConfig queryDockerServiceConfigByServiceId(String serviceId){
        return dockerServiceConfigDao.queryDockerServiceConfigByServiceId(serviceId);
    }


    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<DockerServiceConfig> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=dockerServiceConfigDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<DockerServiceConfig> items=dockerServiceConfigDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
